package managedbean.SCM.Supplier;

import entity.RawMaterial;
import entity.SCM.Supplier;
import entity.SCM.SupplyItem;
import javax.ejb.EJB;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.SCM.SupplierBeanLocal;

@ManagedBean(name="editSupplier")
@ViewScoped
public class EditSupplierManagedBean implements Serializable {

    public EditSupplierManagedBean() {
    }
    @EJB
    private SupplierBeanLocal supplierBeanLocal;
    private Supplier supplierToEdit;
    private SupplyItem selectedSupplyItem;
    private String statusMessage; 
    private List<SupplyItem> supplyItemList;
    private SupplyItem newSi = new SupplyItem();
    private List<String> materials  = new ArrayList<String>();
    //private String selectedRawMaterial;
    
    public void initView(PhaseEvent event){
        if(supplierToEdit==null){
            supplierToEdit=(Supplier)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSupplier");
            if(supplierToEdit==null){
                System.out.print("No Supplier To Edit***********");
            }else{
                this.setSupplyItemList(supplierToEdit.getSupplyItemList());
            }
        }
    }

    public List<String> getMaterials() {
        List<RawMaterial> rms = supplierBeanLocal.getAllMaterial();
        if(rms!=null){
            for(int i=0;i<rms.size();i++){
                this.materials.add(rms.get(i).getMaterialType());
            }
        }
        return materials;
    }

    public SupplyItem getNewSi() {
        return newSi;
    }

    public void setNewSi(SupplyItem newSi) {
        System.out.print("&&&&&&&&&&"+newSi.getSupplyItemType());
        this.newSi = newSi;
    }

    public List<SupplyItem> getSupplyItemList() {
        return supplyItemList;
    }

    public void setSupplyItemList(List<SupplyItem> supplyItemList) {
        this.supplyItemList = supplyItemList;
    }

    public SupplyItem getSelectedSupplyItem() {
        return selectedSupplyItem;
    }

    public void setSelectedSupplyItem(SupplyItem selectedSupplyItem) {
        this.selectedSupplyItem = selectedSupplyItem;
    }

    public Supplier getSupplierToEdit() {
        return supplierToEdit;
    }

    public void setSupplierToEdit(Supplier supplierToEdit) {
        this.supplierToEdit = supplierToEdit;
    }
    
    public void updateSupplier(ActionEvent event){
        try{
            boolean success = supplierBeanLocal.updateSupplier(supplierToEdit.getSupplierName(), 
                    supplierToEdit.getSupplierAddress(), supplierToEdit.getSupplierPostcode(), supplierToEdit.getSupplierContactNo(),
                    supplierToEdit.getSupplierEmail(), supplierToEdit.getSupplyItemList());
            if(success==true)
                statusMessage = "Supplier Updated Success";
            else
                statusMessage = "Supplier Updated Unsuccessfully"; 
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, 
                    "Update Supplier Result: " + statusMessage + " (Supplier Name is " + supplierToEdit.getSupplierName() + ")", ""));
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void addSupplyItem(ActionEvent event) throws IOException {
        SupplyItem temp = this.newSi;
        this.supplyItemList.add(temp);
        newSi = new SupplyItem();
    }
    
    public void deleteSupplyItem(ActionEvent event){
        System.out.print("&&&&&&&&&&&&");
        if(selectedSupplyItem!=null){
            System.out.print("^^^^^^^^^^^^^");
            SupplyItem temp = this.selectedSupplyItem;
            
            for(int i=0; i<supplyItemList.size(); i++){
                if(supplyItemList.get(i).getSupplyItemType().equals(temp.getSupplyItemType())){
                    this.supplyItemList.remove(i);
                }
            }
        }
    }
}
